﻿using System.Text;

namespace LetiLib.Device
{
    public class SMSDevice : BaseDevice
    {
        // SMS info
        public string PhoneNumber{get;set;}// PhoneNumber
        public SMS_MODE Mode{get;set;} // SMS mode

        private SMSSender smsSender = SMSSender.Instance;
        /*
         * Constructor routies
         */
        /// <summary>
        /// Default constructor
        /// </summary>
        public SMSDevice():base()
        {
            PhoneNumber = "0";
            Mode = SMS_MODE.PLAINTEXT;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="pass"></param>
        /// <param name="phonenumber"></param>
        /// <param name="mode"></param>
        public SMSDevice(string name, string imei,string pass, string phonenumber, SMS_MODE mode)
            : base(name, imei,DEVICE_TYPE.SMS, pass)
        {
            PhoneNumber = phonenumber;
            Mode = mode;
        }

        // IBaseDevice implementation
        public override string ToXML()
        {
            return "SMS xml";
        }

        // IConnectable implementation
        public override string open()
        {
            if (smsSender.isOpenned) return "OK";

            smsSender.Connect();
            return "OK";
            
        }

        public override void close()
        {
            // no need to close RS232 connection, it is closes itself

        }

        public override string writeData(byte[] data, int length)
        {
            string message = Encoding.UTF8.GetString(data, 0, length);
            smsSender.sendMessage(PhoneNumber, message, 1);

            return "OK";
        }
    }
    public enum SMS_MODE
    {
        PLAINTEXT,
        PDU
    }
}
